package com.chixing.Server.imp;

import com.chixing.Mapper.CameraMapper;
import com.chixing.Mapper.OrderMapper;
import com.chixing.Server.OrderServer;
import com.chixing.pojo.Camera;
import com.chixing.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class OrderServerImp implements OrderServer {
    // 注入mapper
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CameraMapper  cameraMapper;

    /**
     * 新增订单
     * @param order 订单信息
     * @return 订单是否添加成功
     */
    @Override
    public Boolean insert(Order order) {
        LocalDateTime  now = LocalDateTime.now();// 获取当前时间
        //设置yyyy-mm-dd hh:mm:ss
        order.setCreatedAt(now.toString());
        int result = orderMapper.insert(order);
        return result > 0;
    }
    /**
     * 查询所有订单
     * @param start 起始位置
     * @param pageSize 每页显示的记录数
     * @return 订单列表
     */
    @Override
    public List<Order> queryAllByLimit(int start, int pageSize) {
        start=start-1;
        List<Order> orders = orderMapper.queryAllByLimit(start, pageSize);
        for (Order order : orders){
            // 获取订单id
            String id =order.getCameras();

            // 根据id查询出对应的相机信息
            Camera camera = cameraMapper.queryById(id);
            // 设置相机信息
            order.setCamera(camera);
        }
        return orders;
    }
}
